package com.example.rbacmanage.controller.background;

import com.alibaba.druid.util.StringUtils;
import com.example.rbacmanage.common.Result;
import com.example.rbacmanage.entity.app.Blog;
import com.example.rbacmanage.service.app.BlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/background/blog")
public class BlogController {
    @Autowired
    private BlogService blogService;

    @GetMapping("/list")
    @PreAuthorize("hasAnyAuthority('background:blog:list')")
    public Result page(Blog blog){
        return Result.success(blogService.getPage(blog));
    }

    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Long id){
        return Result.success(blogService.getById(id));
    }


    @PreAuthorize("hasAnyAuthority('background:blog:add')")
    @PostMapping("/add")
    public Result add(@RequestBody Blog blog){
        if (StringUtils.isEmpty(blog.getTop())){
            blog.setTop("0");
        }
        blog.setCreateTime(new Date());
        blogService.save(blog);
        return Result.success();
    }
    @PreAuthorize("hasAnyAuthority('background:blog:update')")
    @PutMapping("/update")
    public Result update(@RequestBody Blog blog){
        blogService.update(blog);
        return Result.success();
    }

    @PreAuthorize("hasAnyAuthority('background:blog:remove')")
    @DeleteMapping("/remove")
    public Result remove(@RequestBody Long[] ids){
        blogService.remove(ids);
        return Result.success();
    }
}
